package com.kakavr.pay.constant;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.kakavr.base.constant.BaseConstant;
import com.kakavr.base.resource.SystemResource;
import com.kakavr.util.SessionUtils;

/**
 * 微信支付常量 来源于微信支付文档
 * @author gang.yang
 *
 */
public class WxConstant {
	public enum SignType {
		MD5, HMACSHA256
	}

	public static final String FAIL = "FAIL";
	public static final String SUCCESS = "SUCCESS";
	public static final String HMACSHA256 = "HMAC-SHA256";
	public static final String MD5 = "MD5";

	public static final String FIELD_SIGN = "sign";
	public static final String FIELD_SIGN_TYPE = "sign_type";
	private static final String APPID = "APPID";
	private static final String MCH_ID = "MCH_ID";
	private static final String UFDODER_URL = "UFDODER_URL";
	private static final String APPKEY = "APPKEY";
	private static final String APPSECRET = "APPSECRET";
	/**
	 * 微信扫码登录状态码
	 */
	public static final String LOGIN_STATE = "39455e0f5599a177e295b2ad124741fa";
	/**
	 * 编码
	 */
	public static final String CHARSET = "UTF-8";
	/**
	 * 商品描述
	 */
	public static final String BODY = "充值卡卡vr";
	/**
	 * 交易类型
	 */
	public static final String TRADE_TYPE = "NATIVE";
	
	/**
	 * 商家端小程序APPID
	 */
	public static final String XCC_APPID = "wxa12476082ce9a40d";
	/**
	 * 商家端小程序APPID
	 */
	public static final String XCC_SECRET = "cbf3206d83275c79a15a4cadef2a151e";
	

	/**
	 * 公众账号ID
	 * 
	 * @return
	 */
	public static String getAppId() {
		return SystemResource.get(APPID);
	}

	/**
	 * 商户号
	 * 
	 * @return
	 */
	public static String getMchId() {
		return SystemResource.get(MCH_ID);
	}

	/**
	 * 统一下单接口
	 * 
	 * @return
	 */
	public static String getUfdoderUrl() {
		return SystemResource.get(UFDODER_URL);
	}

	/**
	 * 密钥
	 * 
	 * @return
	 */
	public static String getAppKey() {
		return SystemResource.get(APPKEY);
	}

	/**
	 * 获取微信通知地址
	 * 
	 * @return
	 */
	public static String getNotifyUrl() {
		return BaseConstant.getBaseUrl() + "/shop/n/wxpay/wxpayNotify";
	}

	/**
	 * 应用密钥AppSecret,扫码登录获取用户openid
	 * 
	 * @return
	 */
	public static String getAppSecret() {
		return SystemResource.get(APPSECRET);
	}

	/**
	 * 扫码确认登录返回地址
	 * 
	 * @return
	 */
	public static String getUserRedirectUri(String sessionId) {
		String url = BaseConstant.getBaseUrl() + 
				"/shop/n/userLogin/signInByWx?sessionId="+sessionId;
		try {
			return URLEncoder.encode(url, CHARSET);
		} catch (UnsupportedEncodingException e) {
		}
		return url;
	}
	/**
	 * 手机端确认登录返回地址
	 * 地址后面增加一个标识 用于判断跳转的地址是不是吐槽页面
	 * @return
	 */
	public static String getUserMobileRedirectUri() {
		String url = BaseConstant.getBaseUrl() + 
				"/shop/n/userLogin/signInByWxMobile"+"?redirectPage="+SessionUtils.get("redirectPage");
		try {
			return URLEncoder.encode(url, CHARSET);
		} catch (UnsupportedEncodingException e) {
		}
		return url;
	}
	
	/**
	 * 扫码确认店主登录返回地址
	 * 
	 * @return
	 */
	public static String getShopRedirectUri() {
		String url = BaseConstant.getBaseUrl() + 
				"/shop/n/merchant/getOpenId";
		try {
			return URLEncoder.encode(url, CHARSET);
		} catch (UnsupportedEncodingException e) {
		}
		return url;
	}

	/**
	 * 返回扫码地址
	 * 
	 * @return
	 */
	public static String getOAuthUrl(String redirectUrl) {
		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
				+ getAppId() + "&redirect_uri=" + redirectUrl
				+ "&response_type=code&scope=snsapi_userinfo&state=" + LOGIN_STATE
				+ "#wechat_redirect";
	}

	/**
	 * 获取会话token的URL地址
	 * 
	 * @param code
	 * @return
	 */
	public static String getAccessTokenUrl(String code) {
		return "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
				+ getAppId() + "&secret=" + getAppSecret() + "&code=" + code
				+ "&grant_type=authorization_code";
	}
	/**
	 * 获取用户信息的URL地址
	 * 
	 * @param code
	 * @return
	 */
	public static String getUserInfoUrl(Object token,Object openid) {
		 return "https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openid;
	}

	/**
	 * 小程序获取openID
	 * 
	 * @param code
	 * @return
	 */
	public static String getXccOpenidUrl(String code) {
		return "https://api.weixin.qq.com/sns/jscode2session?appid="+XCC_APPID+"&secret="+XCC_SECRET+"&js_code="+code+"&grant_type=authorization_code";
	}
}
